package com.yangrd.helper.crudhelper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yangrd.helper.crudhelper.api.CrudCtrlController;
import com.yangrd.helper.crudhelper.api.CrudPageController;
import com.yangrd.helper.crudhelper.core.crud.ctrl.CrudCtrlService;
import com.yangrd.helper.crudhelper.core.crud.ctrl.SpecHelper;
import com.yangrd.helper.crudhelper.core.crud.ctrl.impl.JpaRepositoryCrudCtrlService;
import com.yangrd.helper.crudhelper.core.crud.ctrl.impl.JpaSpecHelper;
import com.yangrd.helper.crudhelper.core.crud.page.*;
import com.yangrd.helper.crudhelper.core.crud.page.impl.*;
import com.yangrd.helper.crudhelper.core.support.CtrlUrlSupport;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * description: CrudHelperConfig
 *
 * @author: yangrd
 * @date: 2020/10/23
 * @version: 1.0
 */
@Configurable
public class CrudHelperAutoConfiguration {

    @Bean
    public DomainMappingManager domainMappingManager(ApplicationContext applicationContext){
        return new DomainMappingManager(applicationContext);
    }

    @Bean
    public TranslateService bingTranslateService(){
        return new BingTranslateService();
    }

    @Bean
    @Primary
    public TranslateService translateService(List<TranslateService> translateServices){
        return new CacheTranslateService(translateServices);
    }

    @Bean
    public AmisCurdTemplate amisCurdTemplate(TranslateService translateService){
        return new AmisCurdTemplate(translateService);
    }

    @Bean
    public CrudPageFactory crudPageFactory(AmisCurdTemplate amisCurdTemplate){
        return new AmisCrudPageFactory(amisCurdTemplate);
    }

    @Bean
    public CrudPageRepository jdbcCrudPageRepository(JdbcTemplate jdbcTemplate){
        return new JdbcCrudPageRepository(jdbcTemplate);
    }

    @Bean
    public CrudPageRepository memoryCrudPageRepository(){
        return new MemoryCrudPageRepository();
    }

    @Bean
    @Primary
    public CrudPageRepository crudPageRepository(List<CrudPageRepository> crudPageRepositories){
        return new ComposeCrudPageRepository(crudPageRepositories);
    }

    @Bean
    public CrudPageInitService crudPageInitService(CrudPageFactory crudPageFactory, CrudPageRepository crudPageRepository){
        return new SimpleCrudPageInitService(crudPageFactory, crudPageRepository);
    }

    @Bean
    public SpecHelper specHelper(){
        return new JpaSpecHelper();
    }

    @Bean
    public CrudCtrlService crudCtrlService(DomainMappingManager domainMappingManager, ObjectMapper objectMapper, SpecHelper specHelper){
        return new JpaRepositoryCrudCtrlService(domainMappingManager, objectMapper, specHelper);
    }

    @Bean
    public CrudCtrlController controllerHelper(CrudCtrlService crudCtrlService){
        return new CrudCtrlController(crudCtrlService);
    }

    @Bean
    public CrudPageController crudPageController(CrudPageRepository crudPageRepository, DomainMappingManager domainMappingManager, CrudPageInitService crudPageInitService){
        return new CrudPageController(crudPageRepository, domainMappingManager, crudPageInitService);
    }

    @Bean
    public CommandLineRunner initRunner(CrudPageInitService crudPageInitService, DomainMappingManager domainMappingManager){
        return args -> {
            crudPageInitService.init(domainMappingManager.listDomainClass());
        };
    }

    @Bean
    public CrudHelperProperties crudHelperProperties(){
        return new CrudHelperProperties();
    }

    @Bean
    public CtrlUrlSupport ctrlUrlSupport(CrudHelperProperties crudHelperProperties){
        return new CtrlUrlSupport(crudHelperProperties);
    }

/*    @PostConstruct
    public void init(CrudPageInitService crudPageInitService, DomainMappingManager domainMappingManager){
        crudPageInitService.init(domainMappingManager.listDomainClass());
    }*/
}
